package org.zfes.snowier.common.monitor.config;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@Configuration
public class WebConfig  implements WebMvcConfigurer {
	
	@Resource
	private MonitorInterceptor  monitorInterceptor;
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
		//registry.addInterceptor(monitorInterceptor).addPathPatterns("/*")
		//.excludePathPatterns("/login","/doLogin","/logout","/monitor/handler/**");
    }
	
	    @Bean
	    public Gson gson() {
	        GsonBuilder b = new GsonBuilder();
//	        b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
//	        b.registerTypeAdapterFactory(DateTypeAdapter.FACTORY);
//	        b.registerTypeAdapterFactory(TimestampTypeAdapter.FACTORY);
//	        b.registerTypeAdapterFactory(LocalDateTypeAdapter.FACTORY);
//	        b.registerTypeAdapterFactory(LocalDateTimeTypeAdapter.FACTORY);
	        return b.create();
	    }

	    @Override
	    public void configureMessageConverters( List<HttpMessageConverter<?>> converters) {
	    	
//	    	 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//	         FastJsonConfig fastJsonConfig = new FastJsonConfig();
//	         fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//	         //日期格式化
//	         //fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//	         converter.setFastJsonConfig(fastJsonConfig);
//	         converters.add(converter);
	         
	         
	    	
	        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
	        stringConverter.setWriteAcceptCharset(false);
	        stringConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
	        converters.add(stringConverter);
	        converters.add(new ByteArrayHttpMessageConverter());
	        converters.add(new SourceHttpMessageConverter<>());
	        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
	        gsonHttpMessageConverter.setGson(gson());
	        gsonHttpMessageConverter.setSupportedMediaTypes(Arrays .asList(MediaType.APPLICATION_JSON));
	        converters.add(gsonHttpMessageConverter);
	    }
}
